<?php
namespace Common\Model;

/**
 * Movie Model Class
 *
 * @package Common\Model
 * @author lythesia
**/
class MovieModel extends BaseModel {
  protected $tableName = 'movie';

  protected $_link = array(
    'Genre' => array(
      'mapping_type'        => self::MANY_TO_MANY,
      'class_name'          => 'Genre',
      'mapping_name'        => 'genres',
      'foreign_key'         => 'movie_id',
      'relation_foreign_key'=> 'genre_id',
      'mapping_order'       => 'name asc',
      'relation_table'      => 'moviegenre',
      'mapping_fields'      => 'id,name',
    ),

    'Person' => array(
      'mapping_type'        => self::MANY_TO_MANY,
      'class_name'          => 'Person',
      'mapping_name'        => 'persons',
      'foreign_key'         => 'object',
      'relation_foreign_key'=> 'subject',
      'relation_table'      => 'personmovie',
      'mapping_fields'      => 'id,name,poster,group_concat(a.relation) as relation,a.subjectimdbid as imdb,a.subjectdbpediaid as dbpedia',
      'mapping_order'       => 'field(a.relation,"director","actor","writer")', // relation_table aliases as `a`
      'condition'           => '1 group by b.id',   // dirty hack
    ),

    'Director' => array(
      'mapping_type'        => self::MANY_TO_MANY,
      'class_name'          => 'Person',
      'mapping_name'        => 'director',
      'foreign_key'         => 'object',
      'relation_foreign_key'=> 'subject',
      'relation_table'      => 'personmovie',
      'mapping_fields'      => 'id,name,poster',
      'condition'           => 'relation = "director"',
      'mapping_limit'       => '1', // only one director 
    ),

    'Actors' => array(
      'mapping_type'        => self::MANY_TO_MANY,
      'class_name'          => 'Person',
      'mapping_name'        => 'actors',
      'foreign_key'         => 'object',
      'relation_foreign_key'=> 'subject',
      'relation_table'      => 'personmovie',
      'mapping_fields'      => 'id,name,poster',
      'condition'           => 'relation = "actor"',
    ),

    'Crew' => array(
      'mapping_type'        => self::MANY_TO_MANY,
      'class_name'          => 'Person',
      'mapping_name'        => 'crew',
      'foreign_key'         => 'object',
      'relation_foreign_key'=> 'subject',
      'relation_table'      => 'personmovie',
      'mapping_fields'      => 'id,name,poster,group_concat(a.relation) as relation',
      'condition'           => 'relation != "actor" group by b.id',   // dirty hack
      //'relation_fields'     => 'group_concat(a.relation)', // customized attr
    ),

    'Movies' => array(
      'mapping_type'        => self::MANY_TO_MANY,
      'class_name'          => 'Movie',
      'mapping_name'        => 'sim_movies',
      'foreign_key'         => 'mid2',
      'parent_key'          => 'mid1',
      'relation_foreign_key'=> 'mid2',
      'relation_table'      => 'simmoviepair',
      'mapping_fields'      => 'id,title,poster',
      'condition'           => '1 order by sim desc limit 4',
    ),
  );
}
